﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.ManagePage
{
    public partial class EditNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            New @new = null;
            if (!string.IsNullOrEmpty(A.M.GetQueryString("Id")))
                @new = A.Controller.NewsController.GetElementById(Convert.ToInt32(A.M.GetQueryString("Id")));
            cboCategory.DataSource = A.Controller.NewsCategoryController.GetAllCategorySub().Where(i=>i.IsActive);
            cboCategory.DataTextField = "Name";
            cboCategory.DataValueField = "Id";
            cboCategory.DataBind();
            if (@new != null)
            {
                cboCategory.SelectedValue = @new.CategoryId.ToString();
                txtTitle.Text = @new.Title;
                txtDescription.Content = @new.Description;
                txtContent.Content = @new.Detail;
            }
        }

        protected void btnInsertOrUpdate_Click(object sender, EventArgs e)
        {
            New @new;
            if (!string.IsNullOrEmpty(A.M.GetQueryString("Id")))
            {
                @new = A.Controller.NewsController.GetElementById(Convert.ToInt32(A.M.GetQueryString("Id")));
                @new.CategoryId = Convert.ToInt32(cboCategory.SelectedValue);
                @new.Title = txtTitle.Text.Trim();
                @new.Description = txtDescription.Content;
                @new.Detail = txtContent.Content;
                A.Controller.NewsController.Update();
            }
            else
            {
                @new = new New();
                @new.CategoryId = Convert.ToInt32(cboCategory.SelectedValue);
                @new.Title = txtTitle.Text.Trim();
                @new.Description = txtDescription.Content;
                @new.Detail = txtContent.Content;
                @new.IsActive = true;
                @new.DateTimeCreate = DateTime.Now;
                @new.UserIdCreate = Convert.ToInt32(A.M.GetAccountFromCookies().Id);
                A.Controller.NewsController.Insert(@new);
            }
        }
    }
}